package avicit.cbb.businessclass.fondsmanage.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import avicit.cbb.techclass.dxcc.JProperties;
import org.springframework.stereotype.Service;

import avicit.cbb.fondsmanage.service.CbbFondsDbIntfsService;
import avicit.platform6.core.jdbc.JdbcAvicit;

/**
 * <p>
 * 金航数码科技有限责任公司
 * </p>
 * <p>
 * 作者：路永刚
 * </p>
 * <p>
 * 邮箱：luyg@avicit.com
 * </p>
 * <p>
 * 创建时间： 2014-9-24 上午9:26:08
 * </p>
 * <p>
 * 类说明：全宗管理接口实现类
 * </p>
 * <p>
 * 修改记录： 张坚：2014-9-24实现方法getCbbFondsDbInfoByIntfs
 * </p>
 */
@Service
public class CbbFondsDbIntfsServiceImpl implements CbbFondsDbIntfsService {

	@Resource
	private JdbcAvicit jdbcAvicit;

	/**
	 * 获取全宗单位数据库配置信息实现
	 * 
	 * @author 路永刚
	 * @param 组织ID
	 * @param 单位ID
	 * @param 系统标识ID
	 * @param 全宗单位ID
	 * @return
	 * @throws Exception
	 *             List<Map<String,Object>>
	 */
	public List<Map<String, Object>> getCbbFondsDbInfoByIntfs(String orgId, String deptId, String sysId, String fondsDeptId) throws Exception {
		// String sql =
		// "select * from CBB_FONDS_DB_V t where t.org_id = ? and t.fonds_dept_id = ? and t.sys_id = ?";
		String sql = "select * from CBB_FONDS_DB_V t where t.org_id = ? and t.sys_id = ?";
		List<Map<String, Object>> list = this.jdbcAvicit.getJdbcTemplate().queryForList(sql, new Object[] {orgId, sysId });
		return list;
	}
	
	/**
	 * 获取全宗单位数据库连接信息
	 * @param param key1：orgId，key2：deptId，key3：sysId，key4：fondsDeptId
	 * @return
	 * @throws Exception Map<String,String>
	 * @throws
	 */
	public Map<String, String> getCbbFondsDbInfoByIntfs(Map<String, Object> param) throws Exception {
		String orgId = "";
		//String deptId= "";
		String sysId = "";
		//String fondsDeptId = "";
		if(param.containsKey("orgId")){
			orgId = String.valueOf(param.get("orgId"));
		}
		/*if(param.containsKey("deptId")){
			deptId = String.valueOf(param.get("deptId"));
		}*/
		if(param.containsKey("sysId")){
			sysId = String.valueOf(param.get("sysId"));
		}
		/*if(param.containsKey("fondsDeptId")){
			fondsDeptId = String.valueOf(param.get("fondsDeptId"));
		}*/
		Map<String, String> map = new HashMap<String, String>();
		String sql = "select * from CBB_FONDS_DB_V t where t.org_id = ? and t.sys_id = ?";
		List<Map<String, Object>> list = this.jdbcAvicit.getJdbcTemplate().queryForList(sql, new Object[] {orgId, sysId });
		String ip = "";// IP
		String port = "";// 端口
		String dbName = "";// 库名
		String dbUser = "";// 用户名
		String dbPwd = "";// 密码
		if (list.size() > 0) {
			Map<String, Object> dbMap = list.get(0);
			ip = String.valueOf(dbMap.get("DB_IP"));
			port = String.valueOf(dbMap.get("DB_PORT"));
			dbName = String.valueOf(dbMap.get("DB_NAME"));
			dbUser = String.valueOf(dbMap.get("DB_USER"));
			dbPwd = String.valueOf(dbMap.get("DB_PASSWORD"));
		}
		String dbConnectionStr = "";//返回的连接字符串
		String dbType = JProperties.getFileProperties("dbType");//数据库部署方式 1：集群 2：单实例
		if(null != dbType && !"".equals(dbType) && "1".equals(dbType)){
			dbConnectionStr = "(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = "+ ip +")(PORT = " + port + "))(CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME = " + dbName + ")))";
		}else{
			dbConnectionStr = ip + ":" + port + ":" + dbName;
		}
		map.put("str", dbConnectionStr);
		map.put("dbUser", dbUser);
		map.put("dbPwd", dbPwd);
		map.put("ipStr", ip);
		return map;
	}
}
